package com.sansan.qiangji.controller.base;

import com.sansan.qiangji.enums.ResultEnum;
import com.sansan.qiangji.exception.ServiceException;
import com.sansan.qiangji.form.base.DeviceAlarmAddForm;
import com.sansan.qiangji.form.base.DeviceAlarmEditForm;
import com.sansan.qiangji.form.base.DeviceAlarmListForm;
import com.sansan.qiangji.form.base.DeviceAlarmListForm;
import com.sansan.qiangji.realize.base.DeviceAlarmReal;
import com.sansan.qiangji.resultVO.ResultVO;
import com.sansan.qiangji.utils.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

@RestController
@Slf4j
@CrossOrigin()
@RequestMapping("/deviceAlarm")
public class DeviceAlarmController {
    
    @Autowired
    private DeviceAlarmReal deviceAlarmReal;
    @PostMapping("/addDeviceAlarm")
    public ResultVO<Object> addDeviceAlarm(@Valid @RequestBody DeviceAlarmAddForm deviceAlarmAddForm, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.error("【报警新增】参数不正确，deviceAlarmAddForm={}",deviceAlarmAddForm);
            throw new ServiceException(ResultEnum.PARAM_ERROR.getCode(), Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        deviceAlarmReal.addDeviceAlarm(deviceAlarmAddForm);
        return ResultVOUtil.success(null);
    }

    @PostMapping("/getDeviceAlarmList")
    public ResultVO<Object> getDeviceAlarmList(@Valid @RequestBody DeviceAlarmListForm form, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.error("【报警查询】参数不正确，deviceAlarmAddForm={}",form);
            throw new ServiceException(ResultEnum.PARAM_ERROR.getCode(), Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        return ResultVOUtil.success(deviceAlarmReal.deviceAlarmList(form));
    }

    @PostMapping("/editDeviceAlarm")
    public ResultVO<Object> editDeviceAlarm(@Valid @RequestBody DeviceAlarmEditForm deviceAlarmEditForm, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.error("【报警修改】参数不正确，deviceAlarmAddForm={}",deviceAlarmEditForm);
            throw new ServiceException(ResultEnum.PARAM_ERROR.getCode(), Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        deviceAlarmReal.editDeviceAlarm(deviceAlarmEditForm);
        return ResultVOUtil.success(null);
    }


    @PostMapping("/deleteDeviceAlarm")
    public ResultVO<Object> deleteDeviceAlarm(@RequestParam(value = "deviceAlarmId") Integer deviceAlarmId){
        deviceAlarmReal.deleteDeviceAlarm(deviceAlarmId);
        return ResultVOUtil.success(null);
    }
}
